// -------- 模块引入区 --------
var yjfk = require('./yjfk'); // 意见反馈
var ck = require('./ck'); // 查看

// -------- 事件管理区 --------
commonUtil.bindEventMap({
    '[role-action="我要反馈"]': _wyfk,
    '[role-action="查看反馈"]': _ckfk
});

// -------- 主代码 --------
init();

// -------- 函数方法区 --------
function init() {
    _initYjfkList();
}

function _initYjfkList() {
    commonUtil.post(SYS_CONTROLLER_PATH + '/query', { full: 1 }, function(r) {
        if (r.datas.length === 0) {
            _renderEmpty();
            return;
        }

        var html = `
            <div class="weui-panel weui-panel_access" style="margin-bottom:70px;">
                <div class="weui-panel__hd">我的反馈意见</div>
                <div class="weui-panel__bd">
                    {{#datas}}
                    <div role-action="查看反馈" role-gid="{{GID}}" class="weui-media-box weui-media-box_text">
                        <strong class="weui-media-box__title">{{FKFLGID_DISPLAY}} {{{SFYHF_VIEW}}}</strong>
                        <p class="weui-media-box__desc">{{NR}}</p>
                        <ul class="weui-media-box__info" aria-hidden="true">
                            <li class="weui-media-box__info__meta" aria-hidden="true" id="js_p4m1_source">创建时间：{{CJSJ}}</li>
                        </ul>
                    </div>
                    {{/datas}}
                </div>
            </div>

            <div class="footer_btn_container"><a href="javascript:void(0);" class="gs-btn gs-btn-danger" style="font-size:18px; width:100%; padding:16px;" role-action="我要反馈">我要反馈</a></div>
        `;

        var datas = r.datas;
        for (var i = 0; i < datas.length; i++) {
            var d = datas[i];
            d.SFYHF_VIEW = d.SFYHF === 1 ? '<span class="mui-badge mui-badge-success" style="float:right;font-size:1rem;">已回复</span>' : '<span class="mui-badge mui-badge-warning" style="float:right;font-size:1rem;">未回复</span>';
        }

        $('#yjfkMainContainer').html(Mustache.render(html, { datas: r.datas }));
    });
}

function _renderEmpty() {
    var html = `
        <div class="weui-panel__hd">我的反馈意见</div>

        <div style="text-align:center;margin-top:150px;">
            <div><img src="{{SYS_STATIC_ROOT_PATH}}/frame/dist/img/empty.png" /><div>
            <div style="font-size:16px;color:#bbb;margin-top:20px;">当前暂无数据</div>

        </div>

        <div class="footer_btn_container"><a href="javascript:void(0);" class="gs-btn gs-btn-danger" style="font-size:18px; width:100%; padding:16px;" role-action="我要反馈">我要反馈</a></div>
     `;
    $('#yjfkMainContainer').html(Mustache.render(html, {
        SYS_STATIC_ROOT_PATH: SYS_STATIC_ROOT_PATH
    }));
}

// 我要反馈
function _wyfk() {
    yjfk.init(function() {
        _initYjfkList();
    });
}

// 查看反馈
function _ckfk(e) {
    var target = $(e.currentTarget);
    var gid = target.attr('role-gid') || '';
    ck.init(gid);
}